<?php
/**
 *费用索赔报审表控制器
 * @author ZhouYP
 */
namespace Admin\Controller;
use Common\Controller\AuditbaseController;
class CostClaimController extends AuditbaseController{

	public function _initialize() {
		parent::_initialize();
		$this->deal_model = D("Common/CostClaim");
		
		$this->check = array('code');
		
		// 当前审批流程
		$this->function = "contract";
		$this->subfunction = "7";
	}
	
	public function index(){
		/**搜索条件**/
		$where = "d.is_deleted <> 1 ";
		$this->indexAuditPage($where);
	}

	//view页面上传审查附件
	public function view_edit(){
		if (!IS_POST) {
            $this->error("非法访问！");
        }
        $url = U(CONTROLLER_NAME . '/' . (I('get.fun') != NULL ? I('get.fun') : 'index'));

        $data = array_merge($_POST, $this->auditAdd());

        //当前用户
        $user = $this->users_model->where('id=' . session('ADMIN_ID'))->find();
        //当前记录
        $ee = $this->deal_model->where("id=" . $data['table_id'])->find();

        if ($this->auditHistoryModel->create() === false) {
            $this->error($this->auditHistoryModel->getError());
        }
        $result = $this->auditHistoryModel->add($data);
        if ($result === false) {
            $this->error("保存失败！");
        }

        //如果当前用户是发起公司拥有审核权限的用户
        if ($user['department_id'] == $ee['add_department'] && $user['role_id'] == 3 && $ee['audit_level'] == 2) {

            $update = array(
                'id'           => $data['table_id'],
                'audit_level'  => $data['audit_status'] == 1 ? intval($data['audit_level']) + 1 : intval($data['audit_level']) - 1,
                'audit_status' => $data['audit_status'] == 2 ? 2 : (intval($this->getAuditFlowLevel()) == intval($data['audit_level']) ? 3 : 1)
            );
            $update = array_merge($_POST, $update);
            $this->deal_model->save($update);
            $this->success("保存成功！", $url);
        } else {

            $role_user = $this->roleuser_model->where('user_id=' . session('ADMIN_ID') . " and code_id=" . $data['table_id'] . " and audit_level=" . $data['audit_level'] . " and function='" . $this->function . "' and subfunction='" . $this->subfunction . "'")->find();

            if (!$role_user) {
                $this->error("没有操作权限！");
            }

            //当前人员是一级审核还是二级审核
            $option_id = $role_user['option_id'];
            $r_u_l     = $role_user['audit_level'];
            //上一层审核
            $pre_audit_level = $role_user['audit_level'] - 1;

            $all_count       = $this->roleuser_model->where("code_id=" . $data['table_id'] . " and audit_level=" . $r_u_l . " and function='" . $this->function . "' and subfunction='" . $this->subfunction . "'")->count();
            $all_agree_count = $this->roleuser_model->where("code_id=" . $data['table_id'] . " and audit_level=" . $r_u_l . " and status=1" . " and function='" . $this->function . "' and subfunction='" . $this->subfunction . "'")->count();

            if ($all_count > 0 && $all_agree_count == $all_count) {
                $this->error("已经全部通过，无需再次审核！");
            }

            if ($data['audit_status'] != 1 && $data['audit_status'] != 2) {
                $this->error("操作状态非法！");
            }

            //在下面判断同意和驳回
            if ($data['audit_status'] == 1) {
                //如果同意

                $role_update = array(
                    'do_time' => time(),
                    'status'  => 1
                );
                $this->roleuser_model->where('id=' . $role_user['id'])->save($role_update);
                //查看页面添加附件
                $this->deal_model->where("id=" . $data['table_id'])->save($_POST);

                $after_all_agree_count = $this->roleuser_model
                    ->where("code_id=" . $data['table_id'] . " and audit_level=" . $r_u_l . " and status=1" . " and function='" . $this->function . "' and subfunction='" . $this->subfunction . "'")
                    ->count();
                if ($all_count > 0 && $after_all_agree_count == $all_count) {
               
                    //如果全部同意
                    // 修改修改主表当前处理层级+1
                    $update = array(
                        'id'           => $data['table_id'],
                        'audit_level'  => intval($data['audit_level']) + 1,
                        'audit_status' => intval($this->getAuditFlowLevel()) == intval($data['audit_level']) ? 3 : 1
                    );

                    $update = array_merge($_POST, $update);
                    $this->deal_model->save($update);
                } else {
                    //如果只是自己同意
                    $update = array(
                        'id'           => $data['table_id'],
                        'audit_level'  => intval($data['audit_level']),
                        'audit_status' => 1
                    );

                    $update = array_merge($_POST, $update);
                    $this->deal_model->save($update);
                }

            } elseif ($data['audit_status'] == 2) {
                //如果驳回
                $role_update = array(
                    'do_time' => time(),
                    'status'  => 0
                );

                //当前层级审核全部初始化
                $this->roleuser_model->where('code_id=' . $data['table_id'] . " and audit_level=" . $r_u_l.  " and function='" . $this->function . "' and subfunction='" . $this->subfunction . "'")->save($role_update);

                //上层审核全部初始化
                $this->roleuser_model->where('code_id=' . $data['table_id'] . " and audit_level=" . $pre_audit_level.  " and function='" . $this->function . "' and subfunction='" . $this->subfunction . "'")->save($role_update);

                $update = array(
                    'id'           => $data['table_id'],
                    'audit_level'  => intval($data['audit_level']) - 1,
                    'audit_status' => 2
                );

                $update = array_merge($_POST, $update);
                $this->deal_model->save($update);
            }
            $this->success("保存成功！", $url);
        }
	}
	
	//分配权限页面
    public function distribution(){
        $level = $_GET['level'];
        //var_dump($level);
        $this->assign("level", $level);
        $this->distribution_base();
    }

    /**
     *  删除 
     */
    public function delete(){
        $id = I('get.id', 0, 'intval');
        if(!$id){
            $this->error("请选择要删除的数据！");
        }
        $data = array(
            'id'    => $id,
            'is_deleted' => 1
        );
        if ($this->deal_model->save($data) !== false) {
            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }
}
?>